
import {Button,Row,Col,Icon,message,Modal} from 'antd';
import {WeaTab,WeaAlertPage, WeaProgress,WeaLocaleProvider,WeaRightMenu,WeaTop} from 'ecCom';
import {inject, observer} from "mobx-react";
import {withRouter} from 'react-router';
import {toJS} from 'mobx';
import {Condition} from '../../list/listCondition';
import ProjectDialog from '../../dialog/projectDialog'
import ShareDialog from '../../dialog/shareDialog'
import ExchangeDialog from '../../dialog/exchangeDialog'
import AddProject from '../../dialog/addProjectDialog'
import {WeaTableNew} from 'comsMobx';
const WeaTable = WeaTableNew.WeaTable;
const getLabel = WeaLocaleProvider.getLabel;

@inject('projectSubStore')
@withRouter
@observer
class ProjectSub extends React.Component {
  constructor(props) {
    super(props);


  }
  componentDidMount(){
    const {prjid} = this.props;
    const {location, projectSubStore } = this.props;
    if(prjid==''||prjid==null){
        projectSubStore.initData(location.query.prjid);
    }else{
        projectSubStore.initData(prjid);
    }
  }
  componentWillReceiveProps(nextProps) {
    const {prjid} = this.props;
    if(prjid==''||prjid==null){
        if (this.props.location.key !== nextProps.location.key) {
            const {location, projectSubStore } = nextProps;
            projectSubStore.initData(location.query.prjid);
        }
    }else{
        if (this.props.prjid !== nextProps.prjid) {
            const { projectSubStore } = nextProps;
            projectSubStore.initData(prjid);
        }
    }
  }
  render() {
        const {projectSubStore} = this.props;
        const {showSearchAd,prjSubStore,prjSubform,condition,prjsubRight:{hasRight,verified},exchangeStore,projectInfoStore,reLoad} = projectSubStore;
        const formParams = prjSubform.getFormParams() || {};
        if (verified && !hasRight) {
            return (<WeaAlertPage ecId={`${this && this.props && this.props.ecId || ''}_WeaAlertPage@1dwhz8`} >
                    <div style={{color : '#000'}}>
                        {getLabel(2012,"对不起，您暂时没有权限！")}
                    </div>
                </WeaAlertPage>
            )
        }

        if (verified && hasRight) {
            return (
                <div style={{backgroundColor:"white",height:"100%"}}>
                    <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@90zknq`} datas={this.getRightMenu()} onClick={this.onRightMenuClick.bind(this)}>
                        <WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@lyq48t`}
                            buttons={this.getButtons()}
                            showDropIcon={true}
                            dropMenuDatas={this.getRightMenu()}
                            onDropMenuClick={this.onRightMenuClick.bind(this)}
                        />
                        <WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@ikbif3`}
                            buttonsAd={this.getAdButtons()}
                            searchType={['base','advanced']}
                            searchsBaseValue={formParams.prjname}
                            setShowSearchAd={bool=>{projectSubStore.setShowSearchAd(bool)}}
                            hideSearchAd={()=> projectSubStore.setShowSearchAd(false)}
                            searchsAd={
                                <div><Condition ecId={`${this && this.props && this.props.ecId || ''}_Condition@gb8nun`} listStore={projectSubStore} form={prjSubform} onEnterSearch={this.onEnterSearch} ></Condition></div>
                            }
                            showSearchAd={showSearchAd}
                            onSearch={v=>{projectSubStore.getPrjSubList()}}
                            onSearchChange={v=>{projectSubStore.appendFormFields({prjname:{value:v}})}}
                        />
                        <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@rp1q1b`} 
                            comsWeaTableStore={prjSubStore}
                            hasOrder={true}
                            needScroll={true}
                            register_table={()=>reLoad(prjSubStore)}
                            getColumns={c=>this.reRenderColumns(c)}
                            onOperatesClick={this.onOperatesClick.bind(this)}
                        />
                        <AddProject ecId={`${this && this.props && this.props.ecId || ''}_AddProject@dqt1or`} contentStore={projectSubStore} isreflesh={true} />
                        <ShareDialog ecId={`${this && this.props && this.props.ecId || ''}_ShareDialog@2otf88`} contentStore={projectSubStore} />
                        <ExchangeDialog ecId={`${this && this.props && this.props.ecId || ''}_ExchangeDialog@2hahui`} contentStore={exchangeStore}  />
                        <ProjectDialog ecId={`${this && this.props && this.props.ecId || ''}_ProjectDialog@2fibqa`} contentStore={projectInfoStore}/>
                    </WeaRightMenu>
                </div>
                )
            }
            return <div></div>
        }

        reRenderColumns(columns){
            columns.forEach((c,i)=>{
                if(c.dataIndex=='finish'){
                    c.render = function(text, record){
                        return  <span  className='wea-prj-progressStyle' >
                            <WeaProgress ecId={`${this && this.props && this.props.ecId || ''}_WeaProgress@1wxd5y@${i}`} percent={record.finish}  strokeColor={record.finishspan} />
                        </span>
                    }
                } else {
                  c.render = function(text, record){
                      let valueSpan = record[c.dataIndex + "span"] !== undefined ? record[c.dataIndex + "span"] : record[c.dataIndex];
                      return <span dangerouslySetInnerHTML={{__html: valueSpan}}></span>
                    }
                }
            })
            return columns;
        }
       
        onOperatesClick(record,index,operate,flag){
            const {projectSubStore} = this.props;
            const {shareStore,exchangeStore,projectInfoStore } = projectSubStore;
            let _href = operate && operate.href ? operate.href : "";
            let fn = _href.replace("javascript:","");
            fn = fn.substring(0,fn.indexOf('('));
            let that = this;
            // saveCapitalId(record.randomFieldId);  //保存资产id
            if(fn != ""){
                if("onShare"==fn){  //共享
                    shareStore.handleShareDialog(true,"prj",record.randomFieldId)
                }else if('onNormal' == fn){   //正常
                    projectSubStore.doSubPlanOpt({method:'normal',prjid:record.randomFieldId})
                }else if('onOver' == fn){   //延期
                    projectSubStore.doSubPlanOpt({method:'delay',prjid:record.randomFieldId})
                }else if('onFinish' == fn){   //完成
                    projectSubStore.doSubPlanOpt({method:'complete',prjid:record.randomFieldId})            
                }else if('onFrozen' == fn){   //冻结
                    projectSubStore.doSubPlanOpt({method:'freeze',prjid:record.randomFieldId})                            
                }else if('onEdit' == fn){   //编辑
                    projectInfoStore.handleDialog(true,record.randomFieldId,{viewtype:'edit',prjid:record.randomFieldId}) ;                                              
                }else if('onListTask' == fn){   //任务列表
                    
                }else if('onDiscuss' == fn){   //相关交流
                    exchangeStore.handleExchangeDialog(true,"prj",record.randomFieldId,{})
                }
            }
        }
        getRightMenu() {
            const { projectSubStore } = this.props;
            const { rightMenu } = projectSubStore;
            let btnArr = [];
            rightMenu && rightMenu.length > 0 && rightMenu.map(m => {
                let disabled = false;
                btnArr.push({
                    icon: <i className={m.menuIcon} />,
                    content: m.menuName,
                    disabled: disabled
                })
            });
            return btnArr
        }
        onRightMenuClick(key) {
            let that = this;
            const { projectSubStore } = this.props;
            const { rightMenu,  prjid, addProjectStore} = projectSubStore;
            rightMenu && rightMenu.length > 0 && rightMenu.map((m, i) => {
                if (Number(key) == i) {
                    if (m.type == "BTN_NEWPRJSUB") { //新建子项目
                        addProjectStore.handleShareDialog(true, { viewtype: 'add', prjid: prjid });
                    }
                
                    if (m.type == "BTN_COLUMN") {  //显示定制
                        projectSubStore.onShowColumn();
                    }
                }
            });
        }
    
    getAdButtons = () => {
        const {projectSubStore} = this.props;
        const {getPrjSubList,setShowSearchAd,clearFormFields} = projectSubStore;
        return [
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@su4x4v@search`} type="primary" onClick={()=>{getPrjSubList();setShowSearchAd(false);}}>{getLabel(197,"搜索")}</Button>),
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@bbs46f@reset`} type="ghost" onClick={()=>{clearFormFields();}}>{getLabel(2022,"重置")}</Button>),
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@sme1zu@cancel`} type="ghost" onClick={()=>{setShowSearchAd(false)}}>{getLabel(201,"取消")}</Button>)
        ];
    }

    getButtons() {
        const { projectSubStore } = this.props;
        const { rightMenu,  prjid, addProjectStore} = projectSubStore;
        let btnArr = [];
        rightMenu && rightMenu.length > 0 && rightMenu.map((m, i) => {
            m.isTop == '1' && btnArr.length < 4 && btnArr.push(
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@qokmqv@${m.type}`} type="primary"
                    onClick={() => {
                        if (m.type == "BTN_NEWPRJSUB") { //新建子项目
                            addProjectStore.handleShareDialog(true, { viewtype: 'add', prjid: prjid });
                        }
                    }}
                >
                    {m.menuName}
                </Button>
            );
        });
        return btnArr;
    }

    onEnterSearch=() =>{
        const { projectSubStore } = this.props;
        projectSubStore.getPrjSubList();
        projectSubStore.setShowSearchAd(false);
    }

}

export default ProjectSub;